package com.yssoft.service.supervise;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.yssoft.mapper.inspection.TInspReportMapper;
import com.yssoft.mapper.purchase.TPurchaseInfoMapper;
import com.yssoft.mapper.purchase.TRecipientAttMapper;
import com.yssoft.mapper.purchase.TRecipientDetailMapper;
import com.yssoft.mapper.traceability.TTraceProcessMapper;
import com.yssoft.mapper.traceability.TTraceQuotaMapper;
import com.yssoft.utils.LayuiUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 * @类名称: PurchaseStatisticsService
 * @类描述: 采购统计service
 * @作者:洪丰
 * @创建时间:2018年6月12日 上午8:46:49
 */
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
@Service
public class PurchaseStatisticsService
{
	@Autowired
	private TPurchaseInfoMapper purchaseInfoMapper;
	@Autowired
    private TRecipientDetailMapper recipientDetailMapper;
	@Autowired
	private TRecipientAttMapper recipientAttMapper;
	@Autowired
	private TInspReportMapper inspReportMapper;
	@Autowired
	private TTraceProcessMapper traceProcessMapper;
	@Autowired
	private TTraceQuotaMapper traceQuotaMapper;
	
	/**
	 * @方法名称: queryPage
	 * @功能描述: 分页查询采购统计
	 * @作者: 洪丰
	 * @创建时间:2018年6月12日 上午10:21:30
	 * @param pageNumber 当前页数
	 * @param pageSize 每页条数
	 * @param startDate 开始时间
	 * @param endDate 结束时间
	 * @param entIds 企业id集合
	 * @param areaId 区域id
	 * @param mClassily 物料分类
	 * @param sId 供应商id
	 * @return JSONObject 返回json
	 */
	public JSONObject queryPage(int pageNumber, int pageSize, String purchaseDate
    		, String entIds, Integer areaId, String mClassily, String sName) {
        PageHelper.startPage(pageNumber,pageSize);//下面第一个查询结果进行分页
        HashMap<String,Object> param = new HashMap<>();
        String startDate = null;
        String endDate = null;
        String[] purchaseDateArray = null;
        if(null != purchaseDate && !"".equals(purchaseDate)){
        	purchaseDateArray = purchaseDate.split(" - ");
        	startDate = purchaseDateArray[0];
        	endDate = purchaseDateArray[1];
        }
        
        param.put("startDate",startDate);
        param.put("endDate",endDate);
        
        if(null != areaId && areaId.compareTo(100000) == 0){
        	param.put("outAreaId","y");
        }else{
        	param.put("areaId",areaId);
        }
        param.put("mClassily",mClassily);
        param.put("sName",sName);
        List<String> entIdList = null;
        if(null != entIds && !"".equals(entIds)){
        	entIdList = Lists.newArrayList();
        	String[] entIdArray = entIds.split(",");
        	for(String entId : entIdArray){
        		entIdList.add(entId);
        	}
        }
        param.put("entIdList",entIdList);
        List<Map<String,Object>> purStatisticsList = purchaseInfoMapper.selectPurStatistics(param);
		int count = purchaseInfoMapper.selectPurStatisticsCount(param);
        JSONObject jsonObject =LayuiUtil.getJSONObjectByMap(purStatisticsList,count,"","");
        return jsonObject;
    }
	
	/**
	 * @方法名称: queryRecipient
	 * @功能描述: 获取收货记录
	 * @作者: 洪丰
	 * @创建时间:2018年6月12日 下午3:05:42
	 * @param purDetailId
	 * @return JSONObject
	 */
	public JSONObject queryRecipient(Integer purDetailId) {
		List<Map<String, Object>> reciveDocumentList = recipientDetailMapper.selectByPurchaseDetailId(purDetailId);
        for (Map<String, Object> recivetMap : reciveDocumentList) {
            List<Map<String, Object>> att = recipientAttMapper.selectByReciveId(Integer.parseInt(recivetMap.get("id") + ""));
            recivetMap.put("att", att);
        }
        JSONObject jsonObject =LayuiUtil.getJSONObjectByMap(reciveDocumentList,0,"","");
        return jsonObject;
    }
	
	/**
	 * @方法名称: queryInspectionPage
	 * @功能描述: 获取检验报告列表
	 * @作者: 洪丰
	 * @创建时间:2018年6月13日 上午11:32:24
	 * @param pageNumber
	 * @param pageSize
	 * @param recipientId
	 * @return JSONObject
	 */
	public JSONObject queryInspectionPage(int pageNumber, int pageSize, Integer recipientId) {
        PageHelper.startPage(pageNumber,pageSize);//下面第一个查询结果进行分页
        List<Map<String,Object>> inspectionList = inspReportMapper.selectReportByRecipientId(recipientId);
		int count = inspReportMapper.selectCountByRecipientId(recipientId);
        JSONObject jsonObject =LayuiUtil.getJSONObjectByMap(inspectionList,count,"","");
        return jsonObject;
    }
	
	/**
	 * @方法名称: queryTraceGather
	 * @功能描述: 获取溯源信息
	 * @作者: 洪丰
	 * @创建时间:2018年6月13日 下午1:44:01
	 * @param recipientId
	 * @return List<Map<String,Object>>
	 */
	public List<Map<String, Object>> queryTraceGather(Integer recipientId) {
        List<Map<String,Object>> straceProcessList = traceProcessMapper.selectTraceProByRecipientId(recipientId);
		for(Map<String, Object> sgMap : straceProcessList){
			if(null != sgMap){
				Map<String, Object> param = new HashMap<String, Object>();
				param.put("processId", sgMap.get("id"));
				param.put("recipientId", recipientId);
				param.put("shareStat", "1");
				List<Map<String,Object>> straceGatherList = traceQuotaMapper.queryTraceQuotaList(param);
				sgMap.put("straceGatherList", straceGatherList);
			}
		}
        return straceProcessList;
    }
}
